
package com.kingdee.signup;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import com.baidu.mapapi.*;
import com.kingdee.signup.adapter.AddressAdapter;
import com.kingdee.signup.model.SearchData;
import com.kingdee.signup.utils.CommonUtils;
import com.kingdee.signup.utils.ComparatorDistance;
import com.kingdee.signup.utils.NLog;
import com.kingdee.signup.utils.StringUtils;
import com.kingdee.signup.widget.LongClickOverlay;
import com.kingdee.signup.widget.PositionOverlay;
import java.util.*;

// Referenced classes of package com.kingdee.signup:
//            SignProjectActivity

public class PoiSearchActivity extends MapActivity implements
		android.view.View.OnClickListener,
		android.widget.AdapterView.OnItemClickListener {
	
	class MyOverlay extends PoiOverlay {
		public MyOverlay(Activity activity, MapView mapview) {
			super(activity, mapview);
			this$0 = PoiSearchActivity.this;
		}
		
		protected boolean onTap(int i) {
			com.baidu.mapapi.MapView.LayoutParams layoutparams = (com.baidu.mapapi.MapView.LayoutParams) mPopView
					.getLayoutParams();
			layoutparams.point = ((MKPoiInfo) mInfo.get(i)).pt;
			mapView.updateViewLayout(mPopView, layoutparams);
			mPopView.setVisibility(0);
			TextView textview = (TextView) findViewById(2131493030);
			textview.setTextColor(Color.parseColor("#000000"));
			textview.setText(((MKPoiInfo) mInfo.get(i)).name);
			return true;
		}

		public boolean onTap(GeoPoint geopoint, MapView mapview) {
			mPopView.setVisibility(8);
			return super.onTap(geopoint, mapview);
		}

		public void setData(ArrayList arraylist) {
			super.setData(arraylist);
			mInfo = arraylist;
		}

		private ArrayList mInfo;
		final PoiSearchActivity this$0;

	}

	public class MySearchListener implements MKSearchListener {

		public void onGetAddrResult(MKAddrInfo mkaddrinfo, int i) {
			if (mkaddrinfo == null)
				NLog.d("debug",
						"\u6CA1\u6709\u83B7\u53D6\u60F3\u8981\u7684\u4F4D\u7F6E");
			else
				NLog.d("debug", (new StringBuilder("\u5730\u5740\uFF1A"))
						.append(mkaddrinfo.strAddr).toString());
		}

		public void onGetDrivingRouteResult(
				MKDrivingRouteResult mkdrivingrouteresult, int i) {
		}

		public void onGetPoiResult(MKPoiResult mkpoiresult, int x, int y)
        {
		     if (mkpoiresult == null)
		      {
		        PoiSearchActivity.this.mPoiResult = null;
		        PoiSearchActivity.this.data = new ArrayList();
		        SearchData localSearchData3 = new SearchData();
		        localSearchData3.setName(PoiSearchActivity.this.mCurrentAddress);
		        localSearchData3.setAddress(PoiSearchActivity.this.mCurrentAddress);
		        localSearchData3.setLatitudeE6(PoiSearchActivity.this.mLatitudeE6);
		        localSearchData3.setLongitudeE6(PoiSearchActivity.this.mLongitudeE6);
		        localSearchData3.setDistance(0.0D);
		        PoiSearchActivity.this.data.add(localSearchData3);
		        PoiSearchActivity.this.mAdapter.addDataList(PoiSearchActivity.this.data);
		        PoiSearchActivity.this.mAdapter.notifyDataSetChanged();
		        return;
		      }
		      PoiSearchActivity.MyOverlay localMyOverlay = new PoiSearchActivity.MyOverlay(PoiSearchActivity.this, mapView);
		      localMyOverlay.setData(mkpoiresult.getAllPoi());
		      mPoiResult = mkpoiresult;
		      data = new ArrayList();
		      if (data.size() > 0)
		        data.clear();
		      
		      SearchData localSearchData1 = new SearchData();
		      localSearchData1.setName("");
		      localSearchData1.setAddress(mCurrentAddress);
		      localSearchData1.setLatitudeE6(mLatitudeE6);
		      localSearchData1.setLongitudeE6(mLongitudeE6);
		      localSearchData1.setDistance(0.0D);
		      data.add(localSearchData1);
		      
		      //读取有用信息
		      SearchData localSearchData3 = new SearchData();
		      localSearchData3.setName("");
		      localSearchData3.setAddress(CommonUtils.getStringByKey(mContext, "MyAdd"));
		      localSearchData3.setLatitudeE6(Long.parseLong(CommonUtils.getStringByKey(mContext, "Lat")));
		      localSearchData3.setLongitudeE6(Long.parseLong(CommonUtils.getStringByKey(mContext, "Lon")));
		      localSearchData3.setDistance(1.0D);
		      data.add(localSearchData3);  
		      for (int i = 0; ; i++)
		      {
		        if (i >= mkpoiresult.getAllPoi().size())
		        {
		          ComparatorDistance localComparatorDistance = new ComparatorDistance();
		          Collections.sort(PoiSearchActivity.this.data, localComparatorDistance);
		          mapView.getOverlays().add(localMyOverlay);
		          mAdapter.addDataList(data);
		          mAdapter.notifyDataSetChanged();
		          break;
		        }
		        // NLog.i("debug", "-----------数据：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).name + " 地址：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).address + "纬度：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLatitudeE6() + " 经度：" + ((MKPoiInfo)paramMKPoiResult.getAllPoi().get(i)).pt.getLongitudeE6());
		        SearchData localSearchData2 = new SearchData();
		        MKPoiInfo mkinfo= (MKPoiInfo)mkpoiresult.getAllPoi().get(i);
		        localSearchData2.setName((mkinfo).name);
		        localSearchData2.setAddress((mkinfo).address);
		        localSearchData2.setLatitudeE6((mkinfo).pt.getLatitudeE6());
		        localSearchData2.setLongitudeE6((mkinfo).pt.getLongitudeE6());
		        localSearchData2.setDistance(StringUtils.GetShortDistance(mLongitudeE6, mLatitudeE6, (mkinfo).pt.getLongitudeE6(), (mkinfo).pt.getLatitudeE6()));
		        data.add(localSearchData2);
		      }
        }

		public void onGetTransitRouteResult(
				MKTransitRouteResult mktransitrouteresult, int i) {
		}

		public void onGetWalkingRouteResult(
				MKWalkingRouteResult mkwalkingrouteresult, int i) {
		}

		final PoiSearchActivity this$0;

		public MySearchListener() {
			super();
			this$0 = PoiSearchActivity.this;
		}
		
	}

	public PoiSearchActivity() {
		mLocationListener = null;
		mLocationOverlay = null;
	}

	private void initPopview() {
		mPopView = super.getLayoutInflater().inflate(2130903070, null);
		mapView.addView(mPopView, new com.baidu.mapapi.MapView.LayoutParams(-2,
				-2, null, 81));
		mPopView.setVisibility(8);
	}

	protected boolean isRouteDisplayed() {
		return false;
	}

	public void onClick(View view)
    {
	    switch (view.getId())
	    {
		    case 2131493009:
		        finish();
		    default:    
	    }
    }
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(2130903066);
		mBack = (ImageView) findViewById(2131493009);
		mBack.setOnClickListener(this);
		mapManager = new BMapManager(getApplication());
		mContext = this;
		//mapManager.init("A1E59D17A869766A73D60658F3D7BEF7BC69ABA7", null);
		mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(2131493010);
		mListView = (ListView) findViewById(2131493011);
		mListView.setOnItemClickListener(this);
		mAdapter = new AddressAdapter(this);
		mListView.setAdapter(mAdapter);
		mapView.setTraffic(true);
		mapView.setBuiltInZoomControls(true);
		mapView.setDrawOverlayWhenZooming(true);
		initPopview();
		mLocationOverlay = new MyLocationOverlay(this, mapView);
		mapView.getOverlays().add(mLocationOverlay);
		mCurrentAddress = getIntent().getStringExtra("projectlocal_address");
		mLocationListener = new LocationListener() {
		final PoiSearchActivity this$0;
		{
				this$0 = PoiSearchActivity.this;
		}
		
		public void onLocationChanged(Location location) {
				if (location != null) {
					geoPoint = new GeoPoint(
							(int) (1000000D * location.getLatitude()),
							(int) (1000000D * location.getLongitude()));
					NLog.i("debug",
							(new StringBuilder("-----------"))
									.append(geoPoint.getLatitudeE6())
									.append("-------")
									.append(geoPoint.getLongitudeE6())
									.toString());
					mLatitudeE6 = (long) geoPoint.getLatitudeE6();
					mLongitudeE6 = (long) geoPoint.getLongitudeE6();
					mapView.getController().animateTo(geoPoint);
					mapController = mapView.getController();
					LongClickOverlay longclickoverlay = new LongClickOverlay(
							PoiSearchActivity.this);
					mapView.getOverlays().add(longclickoverlay);
					mapView.invalidate();
					mapController.setCenter(geoPoint);
					mapController.setZoom(16);
					MKSearch mksearch = new MKSearch();
					MKSearch.setPoiPageCapacity(50);
					mksearch.init(mapManager, new MySearchListener());
					mksearch.poiSearchNearBy("\u5927", geoPoint, 500);
				}
			}

		};
	}

	protected void onDestroy() {
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
	}

	public void onItemClick(AdapterView adapterview, View view, int i, long l) {
		List list = mAdapter.getmDynamicList();
		Intent intent = new Intent();
		intent.setClass(this, com.kingdee.signup.SignProjectActivity.class);
		intent.putExtra(
				"address_title",
				(new StringBuilder(String.valueOf(((SearchData) list.get(i))
						.getAddress()))).append(
						((SearchData) list.get(i)).getName()).toString());
		double d = (double) ((SearchData) list.get(i)).getLatitudeE6() / 1000000D;
		NLog.d("debug",
				(new StringBuilder("latitude_integer--------")).append(d)
						.toString());
		double d1 = (double) ((SearchData) list.get(i)).getLongitudeE6() / 1000000D;
		NLog.d("debug", (new StringBuilder("longitude--------")).append(d1)
				.toString());
		intent.putExtra("latitude",
				(new StringBuilder(String.valueOf(d))).toString());
		intent.putExtra("longitude",
				(new StringBuilder(String.valueOf(d1))).toString());
		setResult(-1, intent);
		finish();
	}

	public boolean onKeyDown(int i, KeyEvent keyevent) {
		boolean flag;
		if (i == 4) {
			finish();
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	protected void onPause() {
		if (mapManager != null) {
			mapManager.getLocationManager().removeUpdates(mLocationListener);
			mLocationOverlay.disableMyLocation();
			mLocationOverlay.disableCompass();
			mapManager.stop();
		}
		super.onPause();
	}

	protected void onResume() {
		if (mapManager != null) {
			mapManager.getLocationManager().requestLocationUpdates(
					mLocationListener);
			mLocationOverlay.enableMyLocation();
			mLocationOverlay.enableCompass();
			mapManager.start();
		}
		super.onResume();
	}

	public void showPopupWindow(int i, int j) {
		GeoPoint geopoint = mapView.getProjection().fromPixels(i, j);
		long l = geopoint.getLatitudeE6();
		long l1 = geopoint.getLongitudeE6();
		NLog.d("debug", (new StringBuilder("alan-----\u6D4B\u8BD5")).append(l)
				.append("(\u7ECF\u5EA6)").append(l1).append("(\u7EAC\u5EA6)")
				.toString());
		if ((int) StringUtils
				.GetShortDistance(mLongitudeE6, mLatitudeE6, l1, l) <= 500)
			mapView.getOverlays().add(
					new PositionOverlay(geopoint, this, 2130837567));
		else
			Toast.makeText(
					mContext,
					"\u53EA\u80FD\u9009\u62E9500\u7C73\u8303\u56F4\u4EE5\u5185\u7684\u5730\u5740\u6807\u6CE8",
					1).show();
	}

	List data;
	SearchData datas;
	GeoPoint geoPoint;
	private AddressAdapter mAdapter;
	private ImageView mBack;
	private Context mContext;
	private String mCurrentAddress;
	private long mLatitudeE6;
	private ListView mListView;
	LocationListener mLocationListener;
	MyLocationOverlay mLocationOverlay;
	private long mLongitudeE6;
	private MKPoiResult mPoiResult;
	public View mPopView;
	private MapController mapController;
	private BMapManager mapManager;
	private MapView mapView;

}